package cn.lczze.stock.controller.base;

import cn.lczze.stock.enums.StockCountryCodeEnum;
import cn.lczze.stock.mapper.base.StockBaseMapper;
import cn.lczze.stock.model.base.StockBaseModel;
import cn.lczze.stock.service.StockBaseService;
import cn.lczze.stock.service.StockCalService;
import cn.lczze.stock.util.AjaxResult;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/stock/base")
public class StockBaseController {

    @Autowired
    private StockBaseService stockBaseService;

    @Autowired
    private StockCalService stockCalService;

    @Autowired
    private StockBaseMapper stockBaseMapper;

    @PostMapping(value = "/start")
    public AjaxResult StartCreatePoint(@RequestBody JSONObject jParam) {
        JSONArray data = jParam.getJSONArray("data");
        for (Object json : data) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(json), JSONObject.class);
            StockBaseModel stockBaseModel = new StockBaseModel();
            String name = jsonObject.getString("name");
            if (name == null) {
                System.out.println(JSONObject.toJSONString(jsonObject));
                continue;
            }
            stockBaseModel.setName(jsonObject.getString("name"));
            stockBaseModel.setTicker(jsonObject.getString("ticker"));
            stockBaseModel.setCountryCode(jsonObject.getString("country_code"));
            stockBaseModel.setIsActive(jsonObject.getInteger("is_active") == null ? 0 : jsonObject.getInteger("is_active"));
            stockBaseModel.setExchangeCode(jsonObject.getString("exchange_code"));
            stockBaseModel.setCurrencyCode(jsonObject.getString("currency_code"));
            stockBaseMapper.insertStockBase(stockBaseModel.getId(), stockBaseModel.getName(), stockBaseModel.getTicker(), stockBaseModel.getIsActive(), stockBaseModel.getCountryCode(),
                                            stockBaseModel.getExchangeCode(), stockBaseModel.getCurrencyCode(),null);
        }
        return AjaxResult.success();
    }

    @GetMapping(value = "/list")
    public AjaxResult baseList() {
        List<StockBaseModel> stockBaseModels = stockBaseMapper.selectStockBaseByCountry();
        List<StockBaseModel> collect = stockBaseModels.stream().filter(p -> p.getIsActive() == 0).collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

    @GetMapping(value = "/profit/list")
    public AjaxResult profitList() {
        List<JSONObject> jsonObjects = stockBaseService.profitLimitList();
        return AjaxResult.success(jsonObjects);
    }

    @GetMapping(value = "/loss/list")
    public AjaxResult lossList() {
        List<JSONObject> jsonObjects = stockBaseService.lossLimitList();
        return AjaxResult.success(jsonObjects);
    }

    @GetMapping(value = "/cal")
    public AjaxResult getStockDayData() {
        stockCalService.getStockDayData();
        return AjaxResult.success();
    }


//    @GetMapping(value = "/con/cal")
//    public AjaxResult getStockDayDataCon() {
//        stockCalService.getStockDayDataCon();
//        return AjaxResult.success();
//    }

    @GetMapping(value = "/calMany")
    public AjaxResult calMany() {
        stockCalService.calMany();
        return AjaxResult.success();
    }


    @GetMapping(value = "/analysi")
    public AjaxResult al() {
        return AjaxResult.success(stockCalService.getStockAnalysis());
    }



}
